<template>
  <view v-if="goods_info.goods_name" class="goods-detail">
    <!-- 轮播图滚动区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in goods_info.pics" :key="index">
        <image :src="item.pics_big" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区 -->
    <view class="detail">
      <!-- 商品价格 -->
      <view class="goods_price">
        ￥{{goods_info.goods_price}}
      </view>
      <!-- 商品描述和收藏 -->
      <view class="goods_descs">
        <view class="left-descs">
          {{goods_info.goods_name}}
        </view>
        <view class="right_collect">
          <uni-icons type="star" size="30" color="gray"></uni-icons>
          <text class="icon-name">
            收藏
          </text>
        </view>
      </view>
      <!-- 快递信息区 -->
      <view class="courier">
        快递：免运费
      </view>
    </view>
    <!-- 商品图文介绍 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 底部导航栏 -->
    <view class="bottom-nav">
      <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    data() {
      return {
        goods_id: '',
        goods_info: {},
        // 底部导航栏数据
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    computed: {
      ...mapGetters('m_cart', ['total']),
    },
    methods: {
      // 获取商品详情
      async getGoodsDetail(goods_id) {
        this.isLoad = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) {
          return uni.showMsg('获取商品详情数据失败！')
        } else {
          // 将数据进行处理
          res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
            .replace(/webp/g, 'jpg')
          this.goods_info = res.message
        }
      },
      // 预览图片
      preview(index) {
        uni.previewImage({
          current: index,
          urls: this.goods_info.pics.map(i => i.pics_big)
        })
      },
      buttonClick(e) {
        if (e.content.text === '立即购买') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        } else if (e.content.text === '加入购物车') {
          // 处理数据
          const goods = {
            goods_id: this.goods_info.goods_id, //id
            goods_name: this.goods_info.goods_name, //名称
            goods_price: this.goods_info.goods_price, //单价
            goods_small_logo: this.goods_info.goods_small_logo, //图片
            goods_count: 1, //数量
            goods_state: true, //商品勾选状态
          }
          // 加入购物车
          this.addToCart(goods)
          // 改变数量
          this.options[2].info = this.total
        }
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // store模块中的方法
      ...mapMutations('m_cart', ['addToCart'])
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.options[2].info = this.total
      this.getGoodsDetail(options.goods_id)
    }
  }
</script>

<style lang="scss">
  .goods-detail {
    padding-bottom: 50px;
  }

  // 轮播图样式
  swiper {
    height: 750rpx;

    swiper-item {

      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  // 商品信息样式
  .detail {
    display: flex;
    flex-direction: column;
    padding: 10px 0px 10px 10px;

    .goods_price {
      margin: 10px 0;
      color: #c00000;
      font-size: 18px;
    }

    .goods_descs {
      display: flex;
      justify-content: space-between;

      .left-descs {
        margin-right: 10px;
        font-size: 13px;
      }
      // 右侧收藏区域
      .right_collect {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 120px;
        border-left: 1px solid #efefef;
        color: gray;
        font-size: 12px;

        .icon-name {
          margin: 10px 0;
        }
      }
    }
   
    .courier {
      margin: 6px 0;
      color: gray;
      font-size: 12px;
    }
  }
   // 底部导航栏
  .bottom-nav {
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
  }
</style>
